package com.qinhao.loveCrowdFunding.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.internal.util.AlipaySignature;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Configuration
@PropertySource("classpath:alipay.properties")
public class AliPayConfig {
    // appId
    private static String appId = "2021000119641563";
    // 私钥
    private static String privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCXB2879qYIODx2hQBB33/t8Ue98u36WYtLjSUECoi4jvLlTAfiMMlK/7eXvCHfhk88qUa+oFC2KgEOGgQo5Xho0pqEk6lN8JYGBfnyymN621XXWwLIyyjeIrnhOxb8NTCuH+YaLESkeZPG4U23pEtrezx8Pt+u3u0iFbAbOL82v+ABbrwqT9o1b4UpXGe/Qr2zDDkfVXtt6LlztVjPs/D6MZ/6Oy0U01CTYxtBOBLLVvkXKNmzUfSoSAYIchowDfFSdzuQoPVWSxZMuwThHBUULTeS64vgH8wTBqyRoiwNB6hrJ9UE9I5CtW2p3JCQhHzO0weZaCXSruqNt60zWZj3AgMBAAECggEAYNWfeG74naSQ/9zfAV1wBPwC58Nu2mm3akWhSioG526gRmytUVW+epvi9yLlBRB0lYxjk63ukh30dzovGJmozmzaLc8Paq2rQ0lprUYe6pLfQylTa7L3DRZu3/6nCiaAg+RoALgJevuIyKeE0Q5CXTOy4GbfvJuqp4QIWivUTiFOSPj0wZPBq6Lc0Ty8m2XNpko1YcINQB4hdpV0BzDIKEusygP0DUcoxwoBYAe8cpPQ4/uObWpcwcVsI/a/Z6EXsrzWIiKRmpKY8r64aq5lMJEDZlJ3h3541xeqaoVm4ZTyQE30ehF3B9VgtCo9gvZQ9s/v2yuUTLXAUQ34tKcroQKBgQD0HYpfsUF9g53VfLCOlVGo15NhMEow4nU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ZYdaFjbGPiI01TXFyRFTZB1OcpOjvvJVXGzYMx2qwoszMVXB0yg5gMfJk+uv9s5acbj27tLNMr03N3IyXJo98ZaZC6E2RHA1SeX1gZERsnB615N0VesyhxK16QmVhjLJOWNXqUGd5Ra97bnX37g/qEvIxM6TlXp/FvpwxhhTwIDAQAB";
    private static String charset = "utf-8";
    private static String signType = "RSA2";
    private static final Logger log = Logger.getLogger(AliPayConfig.class);

    @Bean
    public AlipayClient alipayClient(){
        //沙箱环境使用https://openapi.alipaydev.com/gateway.do，线上环境使用https://openapi.alipay.com/gateway.do
        return new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do", appId, privateKey, "json", charset, publicKey, signType);
    }
    // 验签
    public static boolean checkSign(HttpServletRequest request) {
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, String> paramsMap = new HashMap<>();
        requestMap.forEach((key, values) -> {
            String strs = "";
            for(String value : values) {
                strs = strs + value;
            }
            log.info(key +"===>"+strs);
            paramsMap.put(key, strs);
        });
        System.out.println();
        //调用SDK验证签名
        try {
            return AlipaySignature.rsaCheckV1(paramsMap, AliPayConfig.publicKey, AliPayConfig.charset, AliPayConfig.signType);
        } catch (AlipayApiException e) {
            e.printStackTrace();
            log.info("*********************验签失败********************");
            return false;
        }
    }
}
